package com.example.Sao.MyActivity;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.Sao.MyBean.Event;
import com.example.Sao.MyReceiver.BootBroadcastReceiver;
import com.example.Sao.R;
import com.example.Sao.Serivce.PCService;
import com.example.Sao.Tools.DeviceSNUtil;
import com.wang.avi.AVLoadingIndicatorView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class PCActivity extends MyBaseActivity {
    public static MyBaseActivity activty;
    private USBReceiver usbReceiver;
    //控件
    private TextView serialNumber;
    private AVLoadingIndicatorView indicatorView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pc);
        //初始化控件
        initview();
        activty = this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置屏幕常亮
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.hardware.usb.action.USB_STATE");
        usbReceiver = new USBReceiver();
        registerReceiver(usbReceiver,intentFilter);
        //打开本地服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android 8.0以上 转为前台服务 避免被扼杀
            startForegroundService(new Intent(this, PCService.class));
        } else {
            //android 8.0以下
            startService(new Intent(this, PCService.class));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (usbReceiver!=null)
        unregisterReceiver(usbReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 绑定控件
     */
    private void initview(){
        serialNumber = findViewById(R.id.NS);
        indicatorView = findViewById(R.id.AVLoadingIndicatorView);
        serialNumber.setText(DeviceSNUtil.getSerialNumber());
        indicatorView.show();
    }

    public class USBReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("android.hardware.usb.action.USB_STATE".equals(intent.getAction())) {
                if (!intent.getExtras().getBoolean("connected"))
                      PCActivity.this.finish();
            }
        }
    }

        /**判断activity在栈里的启动情况
         * @param activity
         * @param context
         * @return
         */
        private boolean isExistMainActivity(Class<?> activity,Context context) {
            Intent intent = new Intent(context, activity);
            ComponentName cmpName = intent.resolveActivity(context.getPackageManager());
            boolean flag = false;
            if (cmpName != null) { // 说明系统中存在这个activity    
                ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> taskInfoList =am.getRunningTasks(10);//
                //这里获取的是APP栈的数量，一般也就两个
                ActivityManager.RunningTaskInfo runningTaskInfo = taskInfoList.get(0);// 只是拿          当前运行的栈
                int numActivities = taskInfoList.get(0).numActivities;
                for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                    if (taskInfo.baseActivity.equals(cmpName)) {// 说明它已经启动了
                        flag = true;
                        break;//跳出循环，优化效率
                    }
                }
            }
            return flag;
        }
}
